from libs.cv2location import Cv2Script
from libs.action import Action


def jcfy(driver, max_try=3):
    action = Action()
    while max_try > 0:
        location = Cv2Script(driver).find_target_in_current_screen("jcfy1.png")
        print(f"找到{len(location)}个目标：", location)
        if len(location) > 7:
            # 检测目标过多,修正屏幕从新识别
            max_try -= 1
            action.swipe_screen(581, 986, 581, 900).action_sleep(3).perform()
            continue
        if location:
            print("正在解除...")
            for xy in location:
                action.tap(*xy).action_sleep(1).tap(2066, 998).action_sleep(2). \
                    tap(1000, 400).perform()
            print("解除完毕")
            action.swipe_screen(581, 986, 581, 600).action_sleep(3).perform()
            max_try = 3
        else:
            print("当前未检测到需解封装备,滑动屏幕")
            action.swipe_screen(581, 986, 581, 600).action_sleep(3).perform()
            max_try -= 1
    print("封印解除结束")

